package com.chenyue.cm.user.client.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.user.client.MallGoodsServiceClient;
import com.chenyue.cm.user.domain.JdGoodsSku;
import com.chenyue.cm.user.service.JdGoodsSkuService;
import com.chenyue.cm.user.service.JdGoodsSpuService;
import com.chenyue.cm.user.service.JdGoodsStockLogService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述: author: chenyue date: 2020-02-24
 */
@Service(value = "MallGoodsClientService")
public class MallGoodsClientServiceImpl implements MallGoodsServiceClient {

    @Resource
    private JdGoodsSpuService goodsSpuService;
    @Resource
    private JdGoodsSkuService goodsSkuService;
    @Resource
    private JdGoodsStockLogService goodsStockLogService;


    @Override
    public Success getSpuDetailById(String spuId) {
        System.out.println("获取SPU信息");
        System.out.println(spuId);
        System.out.println("获取SPU信息");
        return Success.success(goodsSpuService.selectByPrimaryKey(spuId));
    }

    @Override
    public Success getSkuDetailById(String skuId) {
        System.out.println("获取SKU信息");
        System.out.println(skuId);
        System.out.println("获取SKU信息");
        return Success.success(goodsSkuService.selectByPrimaryKey(skuId));
    }

    @Override
    public Success verifyStockInfo(String skuItemListString) {
        System.out.println("校验商品库存信息");
        System.out.println(skuItemListString);
        System.out.println("校验商品库存信息");
        List<Map<String, Object>> skuDetailMapList = JSON.parseObject(skuItemListString, List.class);
        for (Map<String, Object> item : skuDetailMapList) {
            String goodsId = item.get("goodId").toString();
            String skuNum = item.get("skuNum").toString();
            JdGoodsSku sku;
            if (item.containsKey("skuId")) {
                String skuId = item.get("skuId").toString();
                sku = goodsSkuService.selectByPrimaryKey(skuId);  //根据skuId 获取sku信息
            }else{
                LambdaQueryWrapper<JdGoodsSku> w = new LambdaQueryWrapper();
                w.eq(JdGoodsSku::getGoodsId,goodsId);
                sku = goodsSkuService.getOne(w);  //根据skuId 获取sku信息
            }
            if (sku.getSkuStock() < Integer.parseInt(skuNum)) {
                return Success.error("库存不足");
            }
        }
        return Success.success();
    }

    @Override
    public Success changeStockInfo(String skuItemListString) {
        System.out.println("更新商品库存信息Start----------");
        System.out.println(skuItemListString);
        List<Map<String, Object>> skuDetailMapList = JSON.parseObject(skuItemListString, List.class);
        for (Map<String, Object> item : skuDetailMapList) {
            JdGoodsSku sku;
            String skuNum = item.get("skuNum").toString();
            if (item.containsKey("skuId")) {
                String skuId = item.get("skuId").toString();
                sku = goodsSkuService.selectByPrimaryKey(skuId);  //根据skuId 获取sku信息
            }else{
                LambdaQueryWrapper<JdGoodsSku> w = new LambdaQueryWrapper();
                String goodsId = item.get("goodId").toString();
                w.eq(JdGoodsSku::getGoodsId,goodsId);
                sku = goodsSkuService.getOne(w);  //根据skuId 获取sku信息
            }


            /*************************************【*】修改商品规格属性【销量+库存】 ****************************************/
            int originalNum = sku.getSkuStock();  //获取商品原库存
            int changeNum = Integer.parseInt(skuNum);  // 变更数量
            int resultNum = originalNum - changeNum;   // 获取现库存数量 = 原库存数 - 变更数量
            sku.setSkuStock(resultNum);  //库存数
            if (sku.getSkuSales() == null) {
                sku.setSkuSales(0);
            }
            sku.setSkuSales(sku.getSkuSales() + changeNum); //销量 = 原销量 + 变更数量
            goodsSkuService.updateById(sku);  //修改商品规格属性

            /*************************************【*】记录商品库存日志  ****************************************/
            if (item.containsKey("skuId")) {
                String skuId = item.get("skuId").toString();
                goodsStockLogService.saveStockLog(skuId, 2, originalNum, changeNum, resultNum);
            }else{
                goodsStockLogService.saveStockLog("", 2, originalNum, changeNum, resultNum);
            }
        }
        System.out.println("更新商品库存信息End----------");
        return Success.success();
    }
}
